<?php
class Vfriend_User extends Vfriend_Db {
	var $cache;
	var $clear;
	public function __construct()
	{
		$this->cache = new Vfriend_Cache();
		$this->clear = $this->cache->clear;
	}
	public function toObjUser($arrResult, $intType=0) {
		$objUser = null;
		if(isset($arrResult[0])) {
			$objUser->id 			= isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
			$objUser->fullhouseid 	= $this->getFullhouseId($objUser->id);
			$objUser->hex 			= isset($arrResult[0]['hexuserid']) ? $arrResult[0]['hexuserid'] : 0;
			$objUser->vaccountid	= isset($arrResult[0]['vaccountid']) ? $arrResult[0]['vaccountid'] : 0;
			$objUser->username 		= isset($arrResult[0]['username']) ? $arrResult[0]['username'] : '';
			$objUser->email 		= isset($arrResult[0]['email']) ? $arrResult[0]['email'] : '';
			$objUser->birthdate 	= isset($arrResult[0]['birthdate']) ? $arrResult[0]['birthdate'] : '';
			$objUser->ages			= isset($arrResult[0]['ages']) ? $arrResult[0]['ages'] : '';
			$objUser->mobile		= isset($arrResult[0]['mobile']) ? $arrResult[0]['mobile'] : '';
			$objUser->gender		= isset($arrResult[0]['gender']) ? $arrResult[0]['gender'] : '';
			$objUser->firstname		= isset($arrResult[0]['firstname']) ? $arrResult[0]['firstname'] : '';
			$objUser->lastname		= isset($arrResult[0]['lastname']) ? $arrResult[0]['lastname'] : '';
			$objUser->fullnamevn	= $objUser->firstname . ' ' . $objUser->lastname;
			$objUser->fullnameen	= $objUser->lastname . ' ' . $objUser->firstname;	
			if($objUser->fullnamevn == '' || $objUser->fullnameen == '') {
				$objUser->fullnamevn = $objUser->fullnameen = $objUser->username;
			}
			$objUser->postdate		= isset($arrResult[0]['postdate']) ? $arrResult[0]['postdate'] : '';
			$objUser->lastlogin		= isset($arrResult[0]['lastlogin']) ? $arrResult[0]['lastlogin'] : '';
			$objUser->statuses		= isset($arrResult[0]['statuses']) ? $arrResult[0]['statuses'] : 0;
			$objUser->verified		= isset($arrResult[0]['verified']) ? $arrResult[0]['verified'] : 0;
			$objUser->uname_verify	= $objUser->verified&1<<2;
			$objUser->src_id		= isset($arrResult[0]['src_id']) ? $arrResult[0]['src_id'] : 0;
			$objUser->code			= isset($arrResult[0]['code']) ? $arrResult[0]['code'] : '';
			$objUser->imageid		= isset($arrResult[0]['imageid']) ? $arrResult[0]['imageid'] : '';
			$objUser->province		= isset($arrResult[0]['province']) ? $arrResult[0]['province'] : 0;
			$objUser->province_name	= isset($arrResult[0]['province_name']) ? $arrResult[0]['province_name'] : '';
			$objUser->country		= isset($arrResult[0]['country']) ? $arrResult[0]['country'] : 0;
			$objUser->country_name	= isset($arrResult[0]['country_name']) ? $arrResult[0]['country_name'] : COUNTRY_DEFAULT;
			$objUser->image_path	= isset($arrResult[0]['image_path']) ? $arrResult[0]['image_path'] : '';
			if($intType == 1) {
				$objUser->introduce			= isset($arrResult[0]['introduce']) ? $arrResult[0]['introduce'] : '';
				$objUser->addinfo			= isset($arrResult[0]['addinfo']) ? $arrResult[0]['addinfo'] : 0;
				$objUser->addr				= isset($arrResult[0]['address']) ? $arrResult[0]['address'] : '';
				$objUser->state				= isset($arrResult[0]['state']) ? $arrResult[0]['state'] : 'NA';
				$objUser->state_name		= isset($arrResult[0]['state_name']) ? $arrResult[0]['state_name'] : '';
				$objUser->district			= isset($arrResult[0]['district']) ? $arrResult[0]['district'] : -1;
				$objUser->district_name		= isset($arrResult[0]['district_name']) ? $arrResult[0]['district_name'] : '';
				$objUser->personality		= isset($arrResult[0]['personality']) ? $arrResult[0]['personality'] : '';
				$objUser->religion			= isset($arrResult[0]['religion']) ? $arrResult[0]['religion'] : -1;
				$objUser->religion_name		= isset($arrResult[0]['religion_name']) ? $arrResult[0]['religion_name'] : '';
				$objUser->religion_userid	= isset($arrResult[0]['religion_userid']) ? $arrResult[0]['religion_userid'] : 0;
				$objUser->height			= isset($arrResult[0]['height']) ? $arrResult[0]['height'] : 0;
				$objUser->weight			= isset($arrResult[0]['weight']) ? $arrResult[0]['weight'] : 0;
				$objUser->occupation		= isset($arrResult[0]['occupation']) ? $arrResult[0]['occupation'] : 0;
				$objUser->occupation_name	= isset($arrResult[0]['occupation_name']) ? $arrResult[0]['occupation_name'] : '';
				$objUser->occupation_userid	= isset($arrResult[0]['occupation_userid']) ? $arrResult[0]['occupation_userid'] : 0;
				$objUser->education			= isset($arrResult[0]['education']) ? $arrResult[0]['education'] : 0;
				$objUser->education_name	= isset($arrResult[0]['education_name']) ? $arrResult[0]['education_name'] : '';
				$objUser->position			= isset($arrResult[0]['position']) ? $arrResult[0]['position'] : 0;
				$objUser->position_name		= isset($arrResult[0]['position_name']) ? $arrResult[0]['position_name'] : '';
				$objUser->marital			= isset($arrResult[0]['marital']) ? $arrResult[0]['marital'] : 0;
				$objUser->marital_name		= isset($arrResult[0]['marital_name']) ? $arrResult[0]['marital_name'] : '';
				$objUser->look_gender		= isset($arrResult[0]['look_gender']) ? $arrResult[0]['look_gender'] : -1;
				$objUser->look_country		= isset($arrResult[0]['look_country']) ? $arrResult[0]['look_country'] : COUNTRY_DEFAULT;
				$objUser->look_province		= isset($arrResult[0]['look_province']) ? $arrResult[0]['look_province'] : -1;
				$objUser->look_afrom		= isset($arrResult[0]['look_afrom']) ? $arrResult[0]['look_afrom'] : -1;
				$objUser->look_ato			= isset($arrResult[0]['look_ato']) ? $arrResult[0]['look_ato'] : -1;
				$objUser->look_wish			= isset($arrResult[0]['look_wish']) ? $arrResult[0]['look_wish'] : -1;
			}
		}
		return $objUser;
	}
	public function add($strEmail, $strPassword, $strBirthdate, $intGender, $strLastName, $strFirstName, $intSrcType=1, $intSrcId=0) {
		try {
			$intAmount = USER_PLUS_AMOUNT_VFRIEND;
			$this->prepare('call users_add(?,?,?,?,?,?,?,?,?);');
			$this->bindParam(1, $strEmail, PDO::PARAM_STR, 255);
			$this->bindParam(2, $strPassword, PDO::PARAM_STR, 150);
			$this->bindParam(3, $strBirthdate, PDO::PARAM_STR, 10);
			$this->bindParam(4, $intGender, PDO::PARAM_INT, 1);
			$this->bindParam(5, $strLastName, PDO::PARAM_STR, 64);
			$this->bindParam(6, $strFirstName, PDO::PARAM_STR, 64);
			$this->bindParam(7, $intSrcType, PDO::PARAM_INT, 4);
			$this->bindParam(8, $intSrcId, PDO::PARAM_INT, 11);
			$this->bindParam(9, $intAmount, PDO::PARAM_INT, 11);
			$arrResult = $this->fetchAll();
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::add - ' .$e->getMessage());			
			return null;
		}
	}
	public function update($intUserId, $strUsername, $strMobile, $strIntroduce, $intAddInfo) {
		try {
			$this->prepare('call users_update(?,?,?,?,?)');
			$this->bindParam(1, $intUserId		, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strUsername	, PDO::PARAM_STR, 30);
			$this->bindParam(3, $strMobile		, PDO::PARAM_STR, 20);
			$this->bindParam(4, $strIntroduce	, PDO::PARAM_STR);
			$this->bindParam(5, $intAddInfo		, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : -1;
			if($intError == 0) {
				$this->clear->deleteUser($intUserId);
				$this->clear->deleteUserByUsername($strUsername);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::update - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateAddinfo($intUserId) {
		try {
			$this->prepare('call users_update_addinfo(?)');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);
			if($this->execute()) {
				$this->clear->deleteUser($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updateAddinfo - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateStatuses($intUserId,$intStatus=1,$strLogs='',$intSrcId=0) {
		try {
			$this->prepare('call users_update_statuses(?,?,?)');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intStatus, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strLogs, PDO::PARAM_STR, 255);
			if($this->execute()) {
				$this->clear->deleteUser($intUserId);
				if($intSrcId > 0)
					$this->clear->deleteUstat($intSrcId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updateStatuses - ' .$e->getMessage());			
			return null;
		}
	}
	public function updatePassword($intUserId, $strPassword, $strEmail='') {
		try {
			$this->prepare('call users_update_password(?,?)');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strPassword, PDO::PARAM_STR, 150);
			if($this->execute()) {
				$this->clear->deleteUser($intUserId);
				if($strEmail) {
					$this->clear->deleteUserByEmail($strEmail);
				}					
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updatePassword - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateLastlogin($intUserId) {
		try {
			$this->prepare('call users_update_lastlogin(?)');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);
			if($this->execute()) {
				$this->clear->deleteUser($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updateLastlogin - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateEmail($intUserId, $strEmail, $intType=0) {
		try {
			if($intType == 0)
				$this->prepare('call uchanges_update_email(?,?)');
			else 
				$this->prepare('call users_update_email(?,?)');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strEmail	, PDO::PARAM_STR, 150);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deleteUser($intUserId);
				$this->clear->deleteUserByEmail($strEmail);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updateEmail - ' .$e->getMessage());			
			return null;
		}
	}
	
	public function updateMobile($intUserId, $strMobile) {
		try {
			$this->prepare('call users_update_mobile(?,?)');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strMobile	, PDO::PARAM_STR, 11);
			if($this->execute()) {
				$this->clear->deleteUser($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updateMobile - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateUsername($intUserId, $strUsername) {
		try {
			$this->prepare('call users_update_username(?,?)');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strUsername, PDO::PARAM_STR, 30);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deleteUser($intUserId);
				$this->clear->deleteUserByUsername($strUsername);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updateIntroduce - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateFullName($intUserId, $strFirstname, $strLastname) {
		try {
			$this->prepare('call users_update_fullname(?,?,?)');
			$this->bindParam(1, $intUserId		, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strFirstname	, PDO::PARAM_STR, 100);
			$this->bindParam(3, $strLastname	, PDO::PARAM_STR, 100);
			if($this->execute()) {
				$this->clear->deleteUser($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updateFullName - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateIntroduce($intUserId, $strIntroduce) {
		try {
			$this->prepare('call users_update_intro(?,?)');
			$this->bindParam(1, $intUserId		, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strIntroduce	, PDO::PARAM_STR);
			if($this->execute()) {
				$this->clear->deleteUser($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updateIntroduce - ' .$e->getMessage());			
			return null;
		}
	}
	public function updatePersonality($intUserId, $strPersonality) {
		try {
			$this->prepare('call uinformations_update_personality(?,?)');
			$this->bindParam(1, $intUserId		, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strPersonality	, PDO::PARAM_STR);
			if($this->execute()) {
				$this->clear->deleteUser($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updatePersonality - ' .$e->getMessage());			
			return null;
		}
	}
	public function getInfo($intUserId) {
		try {
			$strKey 	= USER_GET . $intUserId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_get(?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_EXPIRED);
			}
			return $this->toObjUser($arrResult);
			
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getFullhouseId($intUserId) {
		try {
			$strKey 	= USER_GET_FULLHOUSE . $intUserId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_get_fullhouseid(?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_FULLHOUSE_EXPIRED);
			}
			return isset($arrResult[0]['fullhouseid']) ? $arrResult[0]['fullhouseid'] : 0;
			
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getFullhouseId - ' .$e->getMessage());			
			return null;
		}
	}
	public function getFull($intUserId) {
		try {
			$strKey 	= USER_GET_FULL . $intUserId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult) || 1) {
				$this->prepare('call users_get_full(?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_FULL_EXPIRED);
			}
			return $this->toObjUser($arrResult,1);
			
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getFull - ' .$e->getMessage());			
			return null;
		}
	}
	public function getFullByEmail($strEmail) {
		try {
			$strKey 	= USER_GET_FULLEMAIL . $strEmail;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_get_fullemail(?)');
				$this->bindParam(1, $strEmail, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_FULLEMAIL_EXPIRED);
			}
			return $this->toObjUser($arrResult);			
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getFullByEmail - ' .$e->getMessage());			
			return null;
		}
	}
	public function getInfoByHexUserId($intHexUserId) {
		try {
			$strKey 	= USER_GET_INFOBYHEXUSERID . $intHexUserId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_get_hexuserid(?)');
				$this->bindParam(1, $intHexUserId, PDO::PARAM_INT, 20);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_INFOBYHEXUSERID_EXPIRED);
			}
			return $this->toObjUser($arrResult);
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getInfoByHexUserId - ' .$e->getMessage());			
			return null;
		}
	}
	public function getInfoByEmail($strEmail) {
		try {
			$strKey 	= USER_GET_INFOBYEMAIL . $strEmail;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_get_email(?)');
				$this->bindParam(1, $strEmail, PDO::PARAM_STR, 255);
				$arrResult = $this->fetchAll();				
				$this->cache->set($strKey, $arrResult, USER_GET_INFOBYEMAIL_EXPIRED);				
			}
			return $this->toObjUser($arrResult);
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getInfoByEmail - ' .$e->getMessage());			
			return null;
		}
	}
	public function getInfoByUsername($strUsername) {
		try {
			$strKey 	= USER_GET_INFOBYUSERNAME . $strUsername;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_get_username(?)');
				$this->bindParam(1, $strUsername, PDO::PARAM_STR, 64);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_INFOBYUSERNAME_EXPIRED);
			}
			return $this->toObjUser($arrResult);
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getInfoByUsername - ' .$e->getMessage());			
			return null;
		}
	}
	public function getInfoByCode($strCode) {
		try {
			$strKey 	= USER_GET_INFOBYCODE . $strCode;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_get_code(?)');
				$this->bindParam(1, $strCode, PDO::PARAM_STR, 255);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_INFOBYCODE_EXPIRED);
			}
			return $this->toObjUser($arrResult);
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getInfoByCode - ' .$e->getMessage());			
			return null;
		}
	}
	
	public function getPassword($strEmail) {
		try {
			$strKey 	= USER_GET_PASSWORD . $strEmail;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_get_password(?);');
				$this->bindParam(1, $strEmail, PDO::PARAM_STR, 150);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_PASSWORD_EXPIRED);
			}
			return isset($arrResult[0]['password']) ? $arrResult[0]['password'] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getPassword - ' .$e->getMessage());			
			return null;
		}
	}
	public function getPreference($intUserId, $isString=1) {
		try {
			$strKey 	= USER_GET_PREFERENCE . $intUserId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call users_get_preference(?);');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, USER_GET_PREFERENCE_EXPIRED);
			}
			if($isString && isset($arrData)) {
				$arrResult = array();
				if($isString == 1) {
					foreach($arrData as $value) {
						$arrResult[] = $value['answerid'];
					}
				
					return (isset($arrResult) && count($arrResult)) ? Vfriend_Function::convertArrToStr($arrResult) : '';
				}elseif($isString == 2) {
					foreach($arrData as $value) {
						$arrResult[$value['answerid']] = $value['answerid'];
					}
					return $arrResult;
				}
			} else {
				return isset($arrData) ? $arrData : null;
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getPreference - ' .$e->getMessage());			
			return null;
		}
	}	
	public function getRelation($intUserId, $intShow=1) {
		try {
			$strKey 	= USER_GET_RELATION . $intUserId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call users_get_relation(?);');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, USER_GET_RELATION_EXPIRED);
			}
			if($intShow == 1 && isset($arrData)) { // string = 1,2,3 ...
				$arrResult = array();
				foreach($arrData as $value) {
					$arrResult[] = $value['targetid'];
				}
				return (isset($arrResult) && count($arrResult)) ? Vfriend_Function::convertArrToStr($arrResult) : '';
			}elseif($intShow == 2 && isset($arrData)) {
				$arrResult = array();
				foreach($arrData as $value) {
					$arrResult[$value['targetid']] = $value['targetid'];
				}
				return $arrResult;
			} else {
				return isset($arrData) ? $arrData : null;
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getPreference - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAddinfo($intUserId) {
		try {
			$strKey 	= USER_GET_ADDINFO . $intUserId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_get_addinfo(?);');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();				
				$this->cache->set($strKey, $arrResult, USER_GET_ADDINFO_EXPIRED);
			}
			return isset($arrResult[0]['addinfo']) ? $arrResult[0]['addinfo'] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getPassword - ' .$e->getMessage());			
			return null;
		}
	}
	public function addUpdateBasic($intUserId, $intReligion, $intOccupation, $intEducation, $intPosition,
									$intMarital, $strAddress='', $intDistrict=0, $intProvince=0, $strState='NA',
									$strCountry='VN', $strPersonality='', $intHeight=-1, $intWeight=-1) {
		try {
			$this->prepare('call uinformation_addupdate(?,?,?,?,?,
														?,?,?,?,?,
														?,?,?,?)');
			$this->bindParam(1, 	$intUserId, 		PDO::PARAM_INT, 11);
			$this->bindParam(2, 	$intReligion, 		PDO::PARAM_INT, 6);
			$this->bindParam(3, 	$intOccupation, 	PDO::PARAM_INT, 6);
			$this->bindParam(4, 	$intEducation,		PDO::PARAM_INT, 6);
			$this->bindParam(5, 	$intPosition, 		PDO::PARAM_INT, 6);
			$this->bindParam(6, 	$intMarital, 		PDO::PARAM_INT, 6);
			$this->bindParam(7, 	$strAddress, 		PDO::PARAM_STR, 150);
			$this->bindParam(8, 	$intDistrict, 		PDO::PARAM_INT, 11);
			$this->bindParam(9, 	$intProvince, 		PDO::PARAM_INT, 11);
			$this->bindParam(10, 	$strState, 			PDO::PARAM_STR, 10);
			$this->bindParam(11, 	$strCountry,		PDO::PARAM_STR, 2);
			$this->bindParam(12,	$strPersonality,	PDO::PARAM_STR);
			$this->bindParam(13, 	$intHeight, 		PDO::PARAM_INT, 3);
			$this->bindParam(14, 	$intWeight, 		PDO::PARAM_INT, 3);
			if($this->execute()) {
				$this->clear->deleteUser($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::addUpdateBasic - ' .$e->getMessage());			
			return null;
		}
	}
	public function getUPreference($intUserId, $intQuestionId, $intType=3) {
		try {
			$strKey 	= USER_GET_UPREFERENCE . $intUserId . $intQuestionId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call upreferences_get(?,?,?)');
				$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
				$this->bindParam(2, $intQuestionId, PDO::PARAM_INT, 11);
				$this->bindParam(3, $intType, 		PDO::PARAM_INT, 4);
				$arrData	= $this->fetchAll();
				$this->cache->set($strKey, $arrData, USER_GET_UPREFERENCE_EXPIRED);
			}
			if($arrData) {
				$arrResult = array();
				foreach($arrData as $value) {
					$arrResult[$value['answerid']]	= $value['answerid'];
				}
				return $arrResult;
			} 
			return null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getUPreference - ' .$e->getMessage());			
			return null;
		}
	}
	public function addUPreference($intUserId, $intQuestionId, $intAnswerId, $intType=0) {
		try {
			$this->prepare('call upreferences_add(?,?,?,?)');
			$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intQuestionId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intAnswerId, 	PDO::PARAM_INT, 11);
			$this->bindParam(4, $intType, 		PDO::PARAM_INT, 4);
			if($this->execute()) {
				$this->clear->deleteUpreference($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::addUPreference - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteUPreference($intUserId, $intType=0) {
		try {
			$this->prepare('call upreferences_delete(?,?)');
			$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intType, 		PDO::PARAM_INT, 4);
			if($this->execute()) {
				$this->clear->deleteUpreference($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::deleteUPreference - ' .$e->getMessage());			
			return null;
		}
	}
	public function addUEWork($intUserId, $strName, $strAddr, $intProvince, $strCountry, $intType) {
		try {
			if(is_null($strAddr)) $strAddr = '';
			if(is_null($intProvince)) $intProvince = 0;
			$this->prepare('call ueworks_add(?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $strName, 		PDO::PARAM_STR, 250);
			$this->bindParam(3, $strAddr, 		PDO::PARAM_STR, 150);
			$this->bindParam(4, $intProvince, 	PDO::PARAM_INT, 11);
			$this->bindParam(5, $strCountry, 	PDO::PARAM_STR, 2);
			$this->bindParam(6, $intType, 		PDO::PARAM_INT, 4);
			$this->execute();
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::addUEWork - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteUEWork($intEWorkId, $intUserId) {
		try {
			$this->prepare('call ueworks_delete(?,?)');
			$this->bindParam(1, $intEWorkId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUserId, PDO::PARAM_INT, 11);
			$this->execute();
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::deleteUEWork - ' .$e->getMessage());			
			return null;
		}
	}
	public function getUEWork($intUserId) {
		try {
			$this->prepare('call ueworks_get(?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$arrResult = $this->fetchAll();
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getUEWork - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateLook($intUserId, $intGender, $strCountry, $intProvince, $intAgeFrom, $intAgeTo, $strWish) {
		try {
			$this->prepare('call uinformations_update_look(?,?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intGender, 	PDO::PARAM_INT, 4);
			$this->bindParam(3, $strCountry, 	PDO::PARAM_STR, 2);
			$this->bindParam(4, $intProvince, 	PDO::PARAM_INT, 11);
			$this->bindParam(5, $intAgeFrom, 	PDO::PARAM_INT, 4);
			$this->bindParam(6, $intAgeTo, 		PDO::PARAM_INT, 4);
			$this->bindParam(7, $strWish, 		PDO::PARAM_STR);
			$this->execute();
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::deleteUEWork - ' .$e->getMessage());			
			return null;
		}
	}
	public function getUserNotification($intUserId) {
		try {
			$strKey 	= USER_GET_USERNOTIFICATION . $intUserId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call unotifications_get(?)');
				$this->bindParam(1, $intUserId, 		PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_USERNOTIFICATION_EXPIRED);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::addUpdateNotification - ' .$e->getMessage());			
			return null;
		}
	}
	public function addUpdateNotification($intUserId, $intBdaskboard, $intBnotification, $intBemail, 
											$intIdaskboard, $intInotification, $intIemail, 
											$intFdaskboard, $intFnotification, $intFemail,
											$intOdaskboard, $intOnotification, $intOemail) {
		try {
		
			$this->prepare('call unotifications_addupdate(?,?,?,?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, 		PDO::PARAM_INT, 11);
			$this->bindParam(2, $intBdaskboard, 	PDO::PARAM_INT, 6);
			$this->bindParam(3, $intBnotification, 	PDO::PARAM_STR, 6);
			$this->bindParam(4, $intBemail, 		PDO::PARAM_INT, 6);
			$this->bindParam(5, $intIdaskboard, 	PDO::PARAM_INT, 6);
			$this->bindParam(6, $intInotification, 	PDO::PARAM_INT, 6);
			$this->bindParam(7, $intIemail, 		PDO::PARAM_INT, 6);
			$this->bindParam(8, $intFdaskboard, 	PDO::PARAM_INT, 6);
			$this->bindParam(9, $intFnotification, 	PDO::PARAM_INT, 6);
			$this->bindParam(10, $intFemail, 		PDO::PARAM_INT, 6);
			$this->bindParam(11, $intOdaskboard, 	PDO::PARAM_INT, 6);
			$this->bindParam(12, $intOnotification, PDO::PARAM_INT, 6);
			$this->bindParam(13, $intOemail, 		PDO::PARAM_INT, 6);
			$isResult = $this->execute();
			if($isResult) {
				$this->clear->deleteUserNotification($intUserId);
			}
			return $isResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::addUpdateNotification - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAccount($intUserId, $intType=1) {
		try {			
			$strKey = USER_GET_ACCOUNT . $intUserId . $intType;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				if($intType == 2) $intUserId = $intUserId * (-1);
				$this->prepare('call accounts_get(?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intType, PDO::PARAM_INT, 4);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_ACCOUNT_EXPIRED);
			}
			return isset($arrResult[0]['amount']) ? $arrResult[0]['amount'] : 0;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getAccount - ' .$e->getMessage());			
			return null;
		}
	}	
	public function getPartner($intUserId,&$intType=0) {
		try {
			$strKey = USER_GET_PARTNER . $intUserId . $intType;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call urelations_get_partner(?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intType, PDO::PARAM_INT, 4);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_PARTNER_EXPIRED);
			}
			$intType = isset($arrResult[0]['type']) ? $arrResult[0]['type'] : 0;
			return isset($arrResult[0]['targetid']) ? $arrResult[0]['targetid'] : 0;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getPartner - ' .$e->getMessage());			
			return null;
		}
	}
	public function getUserStat($intUserId) {
		try {
			$strKey = USER_GET_USERSTAT . $intUserId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ustats_get(?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_USERSTAT_EXPIRED);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getUserStat - ' .$e->getMessage());			
			return null;
		}
	}
	public function checkLike($intUserId, $intTargetId) {
		try {
			$strKey = USER_CHECK_LIKE . $intUserId . $intTargetId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ulikes_check(?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_CHECK_LIKE_EXPIRED);
			}
			return isset($arrResult[0]['rCount']) ? $arrResult[0]['rCount'] : 0;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::checkLike - ' .$e->getMessage());			
			return null;
		}
	}
	public function addLike($intUserId, $intTargetId) {
		try {
			$this->prepare('call ulikes_add(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
			$arrResult	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteUstat($intTargetId);
				$this->clear->deleteLike($intUserId, $intTargetId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::addLike - ' .$e->getMessage());			
			return null;
		}
	}
	public function getRelationType($intUserId, $intTargetId) {
		try {
			$strKey = USER_GET_RELATIONTYPE . $intUserId . $intTargetId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call urelations_get_type(?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, USER_GET_RELATIONTYPE_EXPIRED);
			}
			return isset($arrResult[0]['type']) ? $arrResult[0]['type'] : 0;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::getRelationType - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateUstatConvertVon($intUserId, $intTlike, $intVon) {
		try {
			$this->prepare('call ustats_convert_von(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intTlike, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intVon, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$this->clear->deleteAccount($intUserId);
				$this->clear->deleteUstat($intUserId);
				$objVlogs	= new Vlogs();
				$strContent = Zend_Json::encode(array('von'=>$intVon,'tlike'=>$intTlike,'rate'=>RATE_CONVERT_VON,'type'=>1));
				$objVlogs->insert($intUserId,0,1,$strContent);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updateUstatConvertVon - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateUserTransferVon($intUserId,$intVon) {
		try {
			$this->prepare('call users_transfer_von(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intVon, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intFullhouseId = isset($arrResult[0]['fullhouseid']) ? $arrResult[0]['fullhouseid'] : 0;
				$this->clear->deleteAccount($intUserId);
				$this->clear->deleteAccount($intFullhouseId, 2);				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_User::updateUstatConvertVon - ' .$e->getMessage());			
			return null;
		}
	}
}
?>